import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import(/* webpackChunkName: "home" */ '@/views/Home/Index')
const Category = () => import(/* webpackChunkName: "category" */ '@/views/Category/Index')
const Cart = () => import(/* webpackChunkName: "cart" */ '@/views/Cart/Index')
const User = () => import(/* webpackChunkName: "user" */ '@/views/User/Index')
const Detail = () => import(/* webpackChunkName: "Detail" */'@/views/Detail/Index')
const Login = () => import(/* webpackChunkName: "Login" */ '@/views/Login/Index');
const ProductList = () => import(/* webpackChunkName: "ProductList" */ '@/views/ProductList/Index')

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    meta: {
      index: 1,
      login: false
    },
    component: Home
  },
  {
    path: '/category',
    name: 'Category',
    meta: {
      index: 1,
      login: false
    },
    component: Category    
  },
  {
    path: '/cart',
    name: 'Cart',
    meta: {
      index: 1,
      login: true
    },
    component: Cart
  },
  {
    path: '/user',
    name: 'User',
    meta: {
      index: 1,
      login: true
    },
    component: User
  },
  {
    path: '/detail',
    name: 'Detail',
    meta: {
      index: 2,
      login: true
    },
    component: Detail
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      index: 1,
      login: false
    },
    component: Login
  },
  {
    path: '/list',
    name: 'ProductList',
    meta: {
      index: 2,
      login: false
    },
    component: ProductList
  }
]

export default new VueRouter({
  mode: 'history',
  scrollBehavior(to, from, savedPosition) {
    console.log('savedPosition:', savedPosition)
    if (savedPosition) {
      return savedPosition
    } else {
      return {x: 0, y:0}
    }
  },
  routes
})
